<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" fullscreen center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form class="verify-form" size="medium" :model="dataForm" :rules="dataRule" ref="dataForm" label-width="100px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="部门编码" prop="deptCode">
            <el-input v-model="dataForm.deptCode" placeholder="部门编码" readonly>
              <el-button slot="append" :disabled="!!dataForm.id" @click="deptClickHandle">选择</el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门名称" prop="deptName">
            <el-input v-model="dataForm.deptName" placeholder="部门id" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门电话" prop="iphone">
            <el-input v-model="dataForm.iphone" placeholder="部门电话"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="部门地址" prop="address">
            <el-input v-model="dataForm.address" placeholder="部门地址"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="地址坐标X轴" prop="latitude">
            <el-input v-model="dataForm.latitude" placeholder="地址坐标X轴(纬度)">
              <template slot="append">维度</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="地址坐标Y轴" prop="longitude">
            <el-input v-model="dataForm.longitude" placeholder="地址坐标Y轴(经度)">
              <template slot="append">经度</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="微信标题图" prop="wechatTitlePicture">
            <el-upload
              class="upload-demo"
              action="/pvm/wxImage/uploadFileByOptometry"
              :before-upload="beforeUpload"
              :on-success="uploadSuccess"
              :file-list="fileList"
              :data="{deptId: this.dataForm.deptId}"
              >
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </el-form-item>
        </el-col>
        <el-col :span="12">

        </el-col>
        <el-col :span="24">
          <el-form-item label="微信可见" prop="showFlag">
            <el-radio-group v-model="dataForm.showFlag">
              <el-radio label="1">可见</el-radio>
              <el-radio label="0">不可见</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="medium" @click="visible = false">取消</el-button>
      <el-button size="medium" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>

    <search-dept v-if="deptVisible" ref="deptDialog" @refreshData="getDeptData"></search-dept>
  </el-dialog>
</template>

<script>
    import {getStoreDeptConfigurationObj, addStoreDeptConfigurationObj, putStoreDeptConfigurationObj} from '@/api/mp/storedeptconfiguration'
    import SearchDept from './SearchDept'

    export default {
    data () {
      return {
        visible: false,
        deptVisible: false,
        dataForm: {
          id: 0,
          deptCode: '',
          deptName: '',
          iphone: '',
          address: '',
          latitude: '',
          longitude: '',
          wechatTitlePicture: '',
          showFlag: '1'
        },
        fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}],

        dataRule: {
          deptCode: [
            { required: true, message: '部门编码不能为空', trigger: 'blur' }
          ],
          iphone: [
            { required: true, message: '部门电话不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '部门地址不能为空', trigger: 'blur' }
          ],
          showFlag: [
            { required: true, message: '微信可见标识(1-可见,0-不可见)不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      SearchDept
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getStoreDeptConfigurationObj(this.dataForm.id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 选择部门
      deptClickHandle(){
        this.deptVisible = true
        this.$nextTick(() => {
          this.$refs.deptDialog.init(this.dataForm.deptId)
        })
      },
      // 获取选择部门信息
      getDeptData(data){
        this.$set(this.dataForm,'deptId',data.id)
        this.$set(this.dataForm,'deptCode',data.code)
        this.$set(this.dataForm,'deptName',data.name)

        this.dataForm = {...this.dataForm}
      },
      // 图片上传之前
      beforeUpload(file){
        
      },
      // 图片上传成功
      uploadSuccess(){

      },

      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                putStoreDeptConfigurationObj(this.dataForm).then(data => {
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                });
            } else {
                addStoreDeptConfigurationObj(this.dataForm).then(data => {
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                })
            }
          }
        })
      }
    }
  }
</script>
